/*
 * Created on Feb 25, 2005
 */
package com.scalar.chiptrack.finance.openpo.actions;

import java.util.HashMap;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.scalar.chiptrack.ChipTrackExtendedAction;
import com.scalar.chiptrack.commons.SessionKeys;
import com.scalar.chiptrack.finance.openpo.actioncommands.ReceiptSearchCommandFactory;
import com.scalar.chiptrack.users.ErrorReport;
import com.scalar.chiptrack.utils.Logger;
/**
 * @author Ravikanth
**/
public class ReceiptSearchAction extends ChipTrackExtendedAction
{

	/* 
	 * Receipt Search Action
	 */
	public ActionForward executeRequest(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception
	{
		HashMap errors  = ( HashMap ) request.getAttribute( ERROR_MESSAGES );
		HashMap info  	= ( HashMap ) request.getAttribute( INFO_MESSAGES );
		String action   = request.getParameter( REQUEST_ACTION );
		ActionForward actionForward = mapping.findForward( "receiptSearch" );
		ChipTrackExtendedAction receiptOrderCommand = null;
		
		if( errors == null ){ errors  = new HashMap(); }
		if( info == null ){	info  = new HashMap();}


		//Checking If there is any action specified else redirecting to the search page with out processing any further
		if ( ( action == null ) || ( action.trim().length() == 0 ) )
		{
			//No action is specified
			actionForward = mapping.findForward( "receiptSearch" );
		}
		else
		{
			receiptOrderCommand = ReceiptSearchCommandFactory.getActionCommand( action );
			if ( receiptOrderCommand == null )
			{
				Logger.error( ERROR_COULD_NOT_CREATE_RECEIPT_SEARCH_INSTANCE );
				errors.put( ERROR_INTERNAL_SERVER_ERROR, ERROR_COULD_NOT_CREATE_RECEIPT_SEARCH_INSTANCE );
				actionForward = mapping.findForward( "receiptSearch" );
			}
			else
			{
				try
				{
					actionForward = receiptOrderCommand.execute( mapping, form, request, response );
					errors  = ( HashMap ) request.getAttribute( ERROR_MESSAGES );
					info  	= ( HashMap ) request.getAttribute( INFO_MESSAGES );
				}
				catch ( Exception e )
				{
					Logger.error( ERROR_WHILE_EXECUTING_RECEIPT_SEARCH_ACTION_EXECUTE, e );
					errors.put( ERROR_INTERNAL_SERVER_ERROR, ERROR_WHILE_EXECUTING_RECEIPT_SEARCH_ACTION_EXECUTE );

					pSession.setAttribute( ERROR_REPORT_OBJECT, new ErrorReport( "Error while executing the command", e ) );
					actionForward = mapping.findForward( "search" );
				}
			}
		}
		request.setAttribute( SessionKeys.ERROR_MESSAGES, errors );
		request.setAttribute( SessionKeys.INFO_MESSAGES, info );
		return actionForward;
    }
}
